<?php
include_once(dirname(__FILE__)."/../inc/common.php");
$method = isset($_GET["method"])?$_GET["method"]:"";

$flowClass = new tally_flow();

function _array_column($list, $element) {
    if(!isset($list) || !is_array($list) || empty($list)) {
        return array();
    }
    if(!isset($element) || empty($element)) {
        return array();
    }
    $j = 0;
    $result = array();
    for($i = 0; $i < count($list); $i++) {
        if(isset($list [$i] [$element])) {
            $result [$j] = $list [$i] [$element];
            $j++;
        } else {
            return array();
        }
    }
    return $result;
}

if($method=="listQry") {
    // 所选月收入与支出
    $year = isset($_POST["year"])?$_POST["year"]:null;
    $month = isset($_POST["month"])?$_POST["month"]:null;
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";

    $nextMonthYear = $year;
    $nextMonth = $month+1;
    if($nextMonth>12){
        $nextMonth = 1;
        $nextMonthYear++;
    }
    $startTime = strtotime($year."-".$month."-01");
    $endTime = strtotime($nextMonthYear."-".$nextMonth."-01");

    $thisMonthIn = $flowClass->getCountByTimeRangeGroupByType($userId,
        tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds);
    $thisMonthOut = $flowClass->getCountByTimeRangeGroupByType($userId,
        tally_flow::$INOUT_EXPENSE, $startTime, $endTime, $tagIds);

    array_multisort(_array_column($thisMonthIn,'sumIn'),SORT_DESC,$thisMonthIn);
    array_multisort(_array_column($thisMonthOut,'sumOut'),SORT_DESC,$thisMonthOut);
    ?>
    <!--begin:数据列表-------------------------------------------------------------------------------------------------->
    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">
        <tr>
            <th width="1%">&nbsp;</th>
            <th width="20%">类别</th>
            <th width="20%">金额</th>
            <th width="59%"></th>
        </tr>


        <?php
        if(count($thisMonthIn)>0) {
            ?>
            <tr>
                <td></td>
                <td colspan="4" style="text-align:left;font-weight:bold;">收入</td>
            </tr>
            <?php
        }

        $thisMonthInSum = 0;
        foreach($thisMonthIn as $row){
            $thisMonthInSum += $row["sumIn"];
            ?>
            <TR id="content">
                <TD></TD>
                <TD class="to_left">
                    <a href="javascript:fGoto();" onclick="ReportDetail.openDaily(<?php echo $row["flowType"]?>);"><?php echo $row["name"]?></a>
                </TD>
                <TD class="number">
                    <?php echo number_format($row["sumIn"], 2, ".", "")?>
                </TD>

                <TD></TD>
            </TR>
            <?php
        }
        ?>

        <?php
        if(count($thisMonthOut)>0) {
            ?>
            <tr>
                <td></td>
                <td colspan="4" style="text-align:left;font-weight:bold;">支出</td>
            </tr>
            <?php
        }

        $thisMonthOutSum = 0;
        foreach($thisMonthOut as $row){
            $thisMonthOutSum += $row["sumOut"];
            ?>
            <TR id="content">
                <TD></TD>
                <TD class="to_left">
                    <a href="javascript:fGoto();" onclick="ReportDetail.openDaily(<?php echo $row["flowType"]?>);"><?php echo $row["name"]?></a>
                </TD>
                <TD class="number">
                    <?php echo number_format($row["sumOut"], 2, ".", "")?>
                </TD>

                <TD></TD>
            </TR>
            <?php
        }
        ?>


        <TR  id="content">
            <TD></TD>
            <TD class="to_left">支出小计</TD>
            <TD class="number"><?php echo number_format($thisMonthOutSum, 2, ".", "")?></TD>
            <TD></TD>
            <TD class="number"></TD>
        </TR>




        <TR  id="content">
            <TD></TD>
            <TD class="to_left">收入小计</TD>
            <TD class="number"><?php echo number_format($thisMonthInSum, 2, ".", "")?></TD>
            <TD></TD>
            <TD class="number"></TD>
        </TR>
    </table>

    <?php
}else if($method=="pole") {
    // 所选月每天的收入与支出
    $year = isset($_POST["year"])?$_POST["year"]:null;
    $month = isset($_POST["month"])?$_POST["month"]:null;
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";

    $startTime = strtotime($year."-".$month."-01");
    $endTime = $startTime + 24*3600;
    $categories = array();
    $inList = array();
    $outList = array();
    while(date("m", $endTime)==$month){
        $thisDayIn = $flowClass->getCountByTimeRange($userId,
            tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds);
        $thisDayOut = $flowClass->getCountByTimeRange($userId,
            tally_flow::$INOUT_EXPENSE, $startTime, $endTime, $tagIds);
        $categories[] = date("d", $startTime);
        $inList[] = abs($thisDayIn);
        $outList[] = abs($thisDayOut);

        $startTime += 24*3600;
        $endTime = $startTime + 24*3600;
    }

    echo json_encode(array(
        "categories" => $categories,
        "series" => array(
            array(
                "name" => '收入',
                "data" => $inList

            ), array(
                "name" => '支出',
                "data" => $outList

            )
        ),
    ));
}else{
    $flowClass = new tally_flow();
    $flowList = $flowClass->findFlowByUserId($userId, 0, 0, "", "",
        "", "", 0, 0, "tradeTimeAsc", 0, 1);
    $firstTradeYear = date("Y", strtotime($flowList[0]["tradeTime"]));
    $currentYear = date("Y", time());
    $currentMonth = date("m", time());
    ?>
    <!-- begin:导航条 -->
    <div class="titlebar">
        <h3>
            日常收支表
        </h3>
    </div>
    <!-- end:导航条 -->

    <div class="tab_wrapper">
        <div class="body">
            <!-- begin:搜索区 -->
            <form id="schForm" name="schForm" method="post">
                <div class="searchbar" id="searchbar" style="line-height: 150%; display: block;">
                    <div class="base_condition">
                        <table>
                            <tr>
                                <TD><label for="year">统计年度:</label>
                                    <SELECT name="year" id="year">

                                        <?php
                                        for($i=$firstTradeYear;$i<=$currentYear;$i++) {
                                            ?>
                                            <option value="<?php echo $i;?>" <?php echo $i==$currentYear?"selected":""?>>
                                                <?php echo $i;?>
                                            </option>
                                            <?php
                                        }
                                        ?>

                                    </SELECT>
                                </TD>

                                <TD><label for="month">统计月度:</label>
                                    <SELECT name="month" id="month">

                                        <?php
                                        for($i=1;$i<=12;$i++) {
                                            ?>
                                            <option value="<?php echo $i;?>" <?php echo $i==$currentMonth?"selected":""?>>
                                                <?php echo $i;?>
                                            </option>
                                            <?php
                                        }
                                        ?>

                                    </SELECT>
                                </TD>
                                <td><input type="hidden" id="dateFrom" name="dateFrom" clear="true"/></td>
                                <td><input type="hidden" id="dateTo" name="dateTo" clear="true"/></td>

                                <td><input id="searchBtn" class="button" type="button" value="查询"
                                           onclick="DailyReport.search();Report.clearData();Report.searchChart();"/>
                                </td>
                            </tr>
                        </table>
                    </div>

                    <TABLE class="complex_condition">
                        <TR>
                            <td>
                                <label for="tags">
                                    标签:
                                </label>
                                <input type="text" class="text rd" id="tags" name="tags"
                                       readonly="readonly" onClick="CM.clearValue(this);">
                                <input type="hidden" value="" name="tagId" id="tagId"/>
                                <a title="选择项目" href="#"
                                   onclick="TagPicker.open(this,'1');return false;"><img src="/images/icons/select.gif"
                                                                                         align="absmiddle"/>
                                </a>
                            </td>


                            <TD>
                                <INPUT TYPE="radio" NAME="flag" value="1" checked hidden>
                            </TD>

                        </tr>


                    </TABLE>

                </div>
            </form>
            <!-- end:搜索区 -->

            <div class="report_bar">
                <span>数据列表</span>
            </div>

            <div id="dataView" class="data">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>


            <div class="report_bar">
                <span>数据图表</span>
            </div>


            <div id="chartView" class="chartView">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>


            <!-- end:---------主数据区 -->
        </div>
    </div>
    <?php
}
?>
